package org.aeon.colorfulplanebattle.view.panel;

import org.aeon.colorfulplanebattle.view.GameState;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;

import static org.aeon.colorfulplanebattle.constant.GameStateE.*;

/**
 * @author: MissingAeon
 * @date: 2024/1/10 15:22
 * @description:
 */
public class WelcomePage extends JPanel {
    private static final ImageIcon icon;

    static {
        InputStream resource = WelcomePage.class.getClassLoader().getResourceAsStream("img/qita/huanyin.gif");
        ImageIcon read = null;
        try {
            if (resource != null) {
                read = new ImageIcon(ImageIO.read(resource));
            }
        } catch (IOException ignored) {
        }
        icon = (read);
    }

    public WelcomePage(GameState gameState) {
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        this.setPreferredSize(new Dimension(300, 200));
        // 第一层
        JLabel title = new JLabel("彩色飞机射击游戏v0.1");
        // 第二层
        JLabel icon = new JLabel(WelcomePage.icon);
        JLabel welcomeMsg = new JLabel("欢迎游玩”彩色飞机射击游戏“，祝您玩的愉快！");
        // 第三层
        JButton newGame = new JButton("新游戏");
        JButton againGame = new JButton("继续游戏");
        JButton finish = new JButton("结束游戏");

        // 绑定事件
        newGame.addActionListener(e -> gameState.setState(NEW));
        againGame.addActionListener(e -> gameState.setState(HEAVY_LOAD));
        finish.addActionListener(e -> gameState.setState(FINISH));

        // 设置水平和垂直组布局
        layout.setHorizontalGroup(
                layout.createSequentialGroup().addGap(20).addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                .addComponent(title)
                                .addComponent(icon)
                                .addComponent(welcomeMsg)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(newGame)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(againGame)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(finish)
                                ))
        );
        layout.setVerticalGroup(
                layout.createSequentialGroup()
                        .addComponent(title)
                        .addComponent(icon)
                        .addComponent(welcomeMsg)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(newGame)
                                .addComponent(againGame)
                                .addComponent(finish)
                        )
        );
    }
}
